﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace AAEngine.Engine.Serialization
{
    public static class TypeUtility
    {
        public static object Instantiate(string className)
        {
            return Instantiate(GetTypeFromName(className));
        }

        public static object Instantiate(Type type)
        {
            if (type.IsArray)
            {
                return Array.CreateInstance(type.GetElementType(), 0);
            }
            if (typeof(String) == type)
            {
                return String.Empty;
            }

            return Activator.CreateInstance(type);
        }

        public static bool HasProperty(object obj, string propertyName)
        {
            Type type = obj.GetType();

            PropertyInfo pi = type.GetProperty(propertyName, BINDING_FLAGS);

            if (pi != null)
            {
                return true;
            }

            FieldInfo fi = type.GetField(propertyName, BINDING_FLAGS);
            if (fi != null)
            {
                return true;
            }

            return false;
        }

        public static void SetProperty(object obj, string propertyName, object value)
        {
            Type type = obj.GetType();

            if (type.IsArray)
            {
                int index = Int32.Parse(propertyName);
                (obj as Array).SetValue(value, index);
            }
            else if (obj is Hashtable)
            {
                (obj as Hashtable)[propertyName] = value;
            }
            else
            {
                PropertyInfo pi = type.GetProperty(propertyName, BINDING_FLAGS);
                if (pi != null)
                {
                    if (pi.PropertyType.IsAssignableFrom(value.GetType()))
                    {
                        pi.SetValue(obj, value, null);
                    }
                    else
                    {
                        pi.SetValue(obj, Convert.ChangeType(value, pi.PropertyType, null), null);
                    }
                }

                FieldInfo fi = type.GetField(propertyName, BINDING_FLAGS);
                if (fi != null)
                {
                    fi.SetValue(obj, value);
                }
            }
        }

        public static object GetProperty(object obj, string propertyName)
        {
            Type type = obj.GetType();
            PropertyInfo pi = type.GetProperty(propertyName, BINDING_FLAGS);
            if (pi != null)
            {
                return pi.GetValue(obj, null);
            }

            FieldInfo fi = type.GetField(propertyName, BINDING_FLAGS);
            if (fi != null)
            {
                return fi.GetValue(obj);
            }

            return null;
        }

        public static string GetClassName(object obj)
        {
            return obj.GetType().FullName;
        }

        public static string GetFieldType(object obj, string propertyName)
        {
            Type type = obj.GetType();
            PropertyInfo pi = type.GetProperty(propertyName, BINDING_FLAGS);

            if (pi != null)
            {
                return pi.PropertyType.FullName;
            }

            FieldInfo fi = type.GetField(propertyName, BINDING_FLAGS);
            if (fi != null)
            {
                return fi.FieldType.FullName;
            }

            return null;
        }

        public static Enum[] GetEnumValues(Type enumType)
        {
            if (enumType.BaseType == typeof(Enum))
            {
                FieldInfo[] info = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
                Enum[] values = new Enum[info.Length];
                for (int i = 0; i < values.Length; ++i)
                {
                    values[i] = (Enum)info[i].GetValue(null);
                }
                return values;
            }
            else
            {
                throw new Exception("Given type is not an Enum type");
            }
        }

#if WINDOWS_PHONE || XBOX
        public static Type GetTypeFromName(string typeName)
        {
            Type type = Type.GetType(typeName);

            if (type != null)
            {
                return type;
            }

            foreach (string assembly in _assemblies)
            {
                type = Type.GetType(typeName + ", " + assembly);
                if (type != null)
                {
                    return type;
                }
            }

            return null;
        }

        public static void RegisterAssembly(string assembly)
        {
            _assemblies.Add(assembly);
        }
        private static List<string> _assemblies = new List<string>();
#else
        public static void RegisterAssembly(string assembly)
        {
            Assembly.Load(assembly);
        }

        public static Type GetTypeFromName(string typeName)
        {
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assembly in assemblies)
            {
                Type type = assembly.GetType(typeName);
                if (type != null)
                {
                    return type;
                }
            }

            return null;
        }
#endif

        private static readonly BindingFlags BINDING_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase;
    }
}
